%Project 3 
%I. Study a plug-in-Bayes decision rule WITHOUT REJECTION using

% Oded Yechiel
clearvars
P1 = 0.35;P2 = 0.65;a22 = 1;a21 = 0;a12 = 1;a11 = 0;

state_in_rand = 247776455;
%Variant = 6;
Variant_1 = 4;
Variant_2 = 5;
%Bivar = I1;
Error_Rate = 0.08;

[M1,M2,M3,M4,Sigma1,Sigma2,Sigma3,Sigma4]=init_data;
M1(Variant_2,:) = [];M1(Variant_1,:) = [];M2(Variant_2,:) = [];M2(Variant_1,:) = [];M3(Variant_2,:) = [];M3(Variant_1,:) = [];M4(Variant_2,:) = [];M4(Variant_1,:) = [];
Sigma1(Variant_2,:) = [];Sigma1(Variant_1,:) = [];Sigma1(:,Variant_2) = [];Sigma1(:,Variant_1) = [];Sigma2(Variant_2,:) = [];Sigma2(Variant_1,:) = [];Sigma2(:,Variant_2) = [];Sigma2(:,Variant_1) = [];Sigma3(Variant_2,:) = [];
Sigma3(Variant_1,:) = [];Sigma3(:,Variant_2) = [];Sigma3(:,Variant_1) = [];Sigma4(Variant_2,:) = [];Sigma4(Variant_1,:) = [];Sigma4(:,Variant_2) = [];Sigma4(:,Variant_1) = [];

d1 = density(6,M1,Sigma1,a11);d2 = density(6,M2,Sigma2,a12);d3 = density(6,M3,Sigma3,a21);d4 = density(6,M4,Sigma4,a22);
w1 = classi(d1,d2,P1);w2 = classi(d3,d4,P2);clearvars -except w1 w2; close all;clc;

data1 = w1.gen_data(10000);
data2 = w2.gen_data(10000);
N = 1500;
ds1 = data1(1:N*w1.P,:);
ds2 = data2(1:N*w2.P,:);
[ds1_norm,ds2_norm,v]= normalize_data(ds1,ds2);

N_test = 1200;
test1 = data1((N+1):(N+N_test*w1.P),:);
for i=1:size(test1,1)
    test1(i,:) = test1(i,:)./v;
end
test2 = data2((N+1):(N+N_test*w2.P),:);
for i=1:size(test2,1)
    test2(i,:) = test2(i,:)./v;
end

N_new = 3500;
new1 = data1((N+1+N_test):(N+N_test+N_new*w1.P),:);
for i=1:size(new1,1)
    new1(i,:) = new1(i,:)./v;
end
new2 = data2((N+1+N_test):(N+N_test+N_new*w2.P),:);
for i=1:size(new2,1)
    new2(i,:) = new2(i,:)./v;
end
ds1 = ds1_norm;
ds2 = ds2_norm;
clearvars -except w1 w2 ds1 ds2 new1 new2 test1 test2

